<?php
namespace haibao\www\data;

use haibao\www\data\BaseMysql;
class ArticleProImage extends BaseMysql{
    
    public function __construct(){
        parent::__construct('\haibao\www\model\data\ArticleProImage');
    }
    
    public function getProductProImage($aid){
        $sql = 'select GoodsId,ImageId from GoodsImage where GoodsId in ('.implode(',',$aid).') and IsCover = 1';
        $result = $this->query($sql,$aid);
        $info = array();
        while($row = $result->fetch_assoc()){
            $info[$row['GoodsId']] = $row['ImageId'];
        }
        return $info;
    }
    
    public function getArticleProImage($aid,$level){
        $sql = 'select a.PicUrl as PicUrl,a.Width as Width,a.Height as Height,a.Level as Level,b.ParentId as ParentId,Art.PublishId as PublishId from ArticleProImage as a LEFT JOIN ArticleTag as b on a.ArticleId=b.ArticleId LEFT JOIN Article AS Art ON a.ArticleId = Art.Id where a.ArticleId=%s and (a.Level=%s or a.Level=0) and a.PicType=%s and b.IsPrimaiy=%s';
        $data = $this->query($sql,array($aid,$level,1,1));
        $result = array();
        $resZero = array();
        while ($data->fetch_assoc()){
            $rank = $data->getData('Level');
            if($rank == $level){
                $result['picUrl'] = $data->getData('PicUrl');
                $result['width'] = $data->getData('Width');
                $result['height'] = $data->getData('Height');
                $result['parentId'] = $data->getData('ParentId');
                $result['PublishId'] = $data->getData('PublishId');
            }elseif($rank == 0){
                $resZero['picUrl'] = $data->getData('PicUrl');
                $resZero['width'] = $data->getData('Width');
                $resZero['height'] = $data->getData('Height');
                $resZero['parentId'] = $data->getData('ParentId');
                $resZero['PublishId'] = $data->getData('PublishId');
            }
        }
        if(empty($result)){
        	if(empty($resZero)){
        		$res = array();
        		$sql = 'SELECT Art.PublishId as PublishId,Img.Width as Width,Img.Height as Height,Img.PicUrl as PicUrl FROM Article as Art LEFT JOIN `ArticleImage` as Img ON Art.Id=Img.ArticleId WHERE Art.Id=%s ORDER BY Img.ArticleContentId ASC,Img.PicOrder ASC limit 1';
        		$data = $this->query($sql,array($aid));
        		while ($data->fetch_assoc()){
        			$res['picUrl'] = $data->getData('PicUrl');
        			$res['width'] = $data->getData('Width');
        			$res['height'] = $data->getData('Height');
        			$res['parentId'] = 6;
        			$res['PublishId'] = $data->getData('PublishId');
        		}
        		return $res;
        	}else{
        		return $resZero;
        	}
        }else{
            return $result;
        }
    }
    
    
    public function getArticleProImageByIds($aid,$level){
        $sql = 'select Art.Id,a.PicUrl as PicUrl,a.Width as Width,a.Height as Height,a.Level as Level,b.ParentId as ParentId,Art.PublishId as PublishId from ArticleProImage as a LEFT JOIN ArticleTag as b on a.ArticleId=b.ArticleId LEFT JOIN Article AS Art ON a.ArticleId = Art.Id where a.ArticleId in ('.implode(',',$aid).') and (a.Level= '.$level.' or a.Level=0) and a.PicType=1 and b.IsPrimaiy=1';
        $data = $this->query($sql,$aid);
        $result = array();
        $resZero = array();
        $rowResult = $rowZero = array();
        while ($data->fetch_assoc()){
            $rank = $data->getData('Level');
            if($rank == $level){
                $result['picUrl'] = $data->getData('PicUrl');
                $result['width'] = $data->getData('Width');
                $result['height'] = $data->getData('Height');
                $result['parentId'] = $data->getData('ParentId');
                $result['PublishId'] = $data->getData('PublishId');
                $rowResult[$data->getData('Id')] = $result;
            }elseif($rank == 0){
                $resZero['picUrl'] = $data->getData('PicUrl');
                $resZero['width'] = $data->getData('Width');
                $resZero['height'] = $data->getData('Height');
                $resZero['parentId'] = $data->getData('ParentId');
                $resZero['PublishId'] = $data->getData('PublishId');
                $rowZero[$data->getData('Id')] = $resZero;
            }
            
        }
        
        if($rowZero && $rowResult){
            foreach($rowZero as $key=>$val){
                foreach($rowResult as $k=>$v){
                    if(!isset($rowResult[$key])){
                        $rowResult[$key] = $rowZero[$key];
                    }
                }
            }
        }
        if(!$rowResult && $rowZero){
            $rowResult = $rowZero;
        }
        if(empty($rowResult)){
            if(empty($rowZero)){
                $res = array();
                $sql = 'SELECT Art.Id,Art.PublishId as PublishId,Img.Width as Width,Img.Height as Height,Img.PicUrl as PicUrl FROM Article as Art LEFT JOIN `ArticleImage` as Img ON Art.Id=Img.ArticleId WHERE Art.Id in ('.implode(',',$aid).') ORDER BY Img.ArticleContentId ASC,Img.PicOrder ASC limit 1';
                $data = $this->query($sql,$aid);
                while ($data->fetch_assoc()){
                    $res['picUrl'] = $data->getData('PicUrl');
                    $res['width'] = $data->getData('Width');
                    $res['height'] = $data->getData('Height');
                    $res['parentId'] = 6;
                    $res['PublishId'] = $data->getData('PublishId');
                    $rowZero[$data->getData('Id')] = $res;
                }
                return $rowZero;
            }else{
                return $rowZero;
            }
        }else{
            return $rowResult;
        }
    }
    
    public function getImageCollectiveProIamge($aid,$type){
        $sql = 'select l.Filename as FileName,l.Width as Width,l.Height as Height,t.ParentId as ParentId,p.ImageType as ImageType from ImageCollectivePro as p LEFT JOIN ImageCollectiveTag as t on p.ImageCollectiveId=t.ImageCollectiveId LEFT JOIN ImageLibrary as l on p.ImageId=l.Id where p.ImageType in('.$type.',0) and p.ImageCollectiveId=%s and t.IsPrimaiy=%s';
        $data = $this->query($sql,array($aid,1));
        $result = array();
        $flag = false;
        while ($data->fetch_assoc()){
        	if($data->getData('ImageType') == $type){
        		$result['picUrl'] = $data->getData('FileName');
        		$result['width'] = $data->getData('Width');
        		$result['height'] = $data->getData('Height');
        		$result['parentId'] = $data->getData('ParentId');
        		$flag = true;
        	}
        	if(!$flag && $data->getData('ImageType') == \haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_ZERO){
        		$result['picUrl'] = $data->getData('FileName');
        		$result['width'] = $data->getData('Width');
        		$result['height'] = $data->getData('Height');
        		$result['parentId'] = $data->getData('ParentId');
        	}
        }
        return $result;
    }
    
    public function getImageCollectiveProIamgeByIds($aid,$type){
        $sql = 'select p.ImageCollectiveId,l.Filename as FileName,l.Width as Width,l.Height as Height,t.ParentId as ParentId,p.ImageType as ImageType from ImageCollectivePro as p LEFT JOIN ImageCollectiveTag as t on p.ImageCollectiveId=t.ImageCollectiveId LEFT JOIN ImageLibrary as l on p.ImageId=l.Id where p.ImageType in('.$type.',0) and p.ImageCollectiveId in ('.implode(',',$aid).') and t.IsPrimaiy=1';
        $data = $this->query($sql,$aid);
        $result = array();
        $flag = false;
        $rowFive = $rowZero = array();
        while ($data->fetch_assoc()){
            if($data->getData('ImageType') == $type){
                $result['picUrl'] = $data->getData('FileName');
                $result['width'] = $data->getData('Width');
                $result['height'] = $data->getData('Height');
                $result['parentId'] = $data->getData('ParentId');
                $result['ImageCollectiveId'] = $data->getData('ImageCollectiveId');
                $flag = true;
                $rowFive[$data->getData('ImageCollectiveId')] = $result;
            }
            if($data->getData('ImageType') == \haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_ZERO){
                $result['picUrl'] = $data->getData('FileName');
                $result['width'] = $data->getData('Width');
                $result['height'] = $data->getData('Height');
                $result['parentId'] = $data->getData('ParentId');
                $result['ImageCollectiveId'] = $data->getData('ImageCollectiveId');
                $rowZero[$data->getData('ImageCollectiveId')] = $result;
            }
        }
        if($rowFive && $rowZero){
            foreach($rowZero as $key=>$val){
                foreach($rowFive as $k=>$v){
                    if(!isset($rowFive[$key])){
                        $rowFive[$key] = $rowZero[$key];
                    }
                }
            }
        }
        if(!$rowFive && $rowZero){
            $rowFive = $rowZero;
        }
        return $rowFive;
    }
    
    
    
    public function getVideoProImage($vid){
        $sql = 'SELECT i.Filename as FileName,i.Width as Width,i.Height as Height FROM `Video` as v LEFT JOIN ImageLibrary as i ON v.PicId=i.Id WHERE v.Id=%s';
        $data = $this->query($sql,array($vid));
        $result = array();
        while ($data->fetch_assoc()){
            $result['picUrl'] = $data->getData('FileName');
            $result['width'] = $data->getData('Width');
            $result['height'] = $data->getData('Height');
        }
        return $result;
    }
    
    public function getVideoProImageByIds($vid){
        $sql = 'SELECT v.Id,i.Filename as FileName,i.Width as Width,i.Height as Height FROM `Video` as v LEFT JOIN ImageLibrary as i ON v.PicId=i.Id WHERE v.Id in ('.implode(',',$vid).')';
        $data = $this->query($sql,$vid);
        $result = $row = array();
        while ($data->fetch_assoc()){
            $result['picUrl'] = $data->getData('FileName');
            $result['width'] = $data->getData('Width');
            $result['height'] = $data->getData('Height');
            $row[$data->getData('Id')] = $result;
        }
        return $row;
    }
    
    public function getFeatureProImage($fid){
        $sql = 'SELECT f.Url as Url,i.Filename as Filename,i.Width as Width,i.Height as Height FROM `FeaturePublish` as f LEFT JOIN ImageLibrary as i ON f.PictureId=i.Id where f.Id=%s';
        $data = $this->query($sql,array($fid));
        $result = array();
        while ($data->fetch_assoc()){
            $result['picUrl'] = $data->getData('Filename');
            $result['width'] = $data->getData('Width');
            $result['height'] = $data->getData('Height');
            $result['Url'] = $data->getData('Url');
        }
        return $result;
    }
    
    public function getFeatureProImageByIds($fid){
        $sql = 'SELECT f.Id,f.Url as Url,i.Filename as Filename,i.Width as Width,i.Height as Height FROM `FeaturePublish` as f LEFT JOIN ImageLibrary as i ON f.PictureId=i.Id where f.Id in ('.implode(',',$fid).')';
        $data = $this->query($sql,$fid);
        $result = $row = array();
        while ($data->fetch_assoc()){
            $result['picUrl'] = $data->getData('Filename');
            $result['width'] = $data->getData('Width');
            $result['height'] = $data->getData('Height');
            $result['Url'] = $data->getData('Url');
            $row[$data->getData('Id')] = $result;
        }
        return $row;
    }
    
    public function getTryProductProImage($id){
        $sql = 'SELECT i.Filename as Filename,i.Width as Width,i.Height as Height FROM `TryProduct` as T LEFT JOIN ImageLibrary as i ON T.PicId=i.Id where T.Id=%s';
        $data = $this->query($sql,array($id));
        $result = array();
        while ($data->fetch_assoc()){
            $result['picUrl'] = $data->getData('Filename');
            $result['width'] = $data->getData('Width');
            $result['height'] = $data->getData('Height');
        }
        return $result;
    }
    
    public function getTryProductProImageByIds($id){
        $sql = 'SELECT T.Id,i.Filename as Filename,i.Width as Width,i.Height as Height FROM `TryProduct` as T LEFT JOIN ImageLibrary as i ON T.PicId=i.Id where T.Id in ('.implode(',',$id).')';
        $data = $this->query($sql,$id);
        $result = $row = array();
        while ($data->fetch_assoc()){
            $result['picUrl'] = $data->getData('Filename');
            $result['width'] = $data->getData('Width');
            $result['height'] = $data->getData('Height');
            $row[$data->getData('Id')] = $result;
        }
        return $row;
    }
    
    public function getArticleTitleById($aid){
        $sql = 'select Title,PublishId from Article where Id=%s';
        $data = $this->query($sql,array($aid));
        $result = array();
        while($data->fetch_assoc()){
            $result['Title'] = $data->getData('Title');
            $result['PublishId'] = $data->getData('PublishId');
        }
        
        return $result;
    }
    
    public function getWapArticleTitleById($aid,$type){
    	$sql = 'select Title,PublishId from WapInfo where InfoId=%s and InfoType=%s';
    	$data = $this->query($sql,array($aid,$type));
    	$result = array();
    	while($data->fetch_assoc()){
    		$result['Title'] = $data->getData('Title');
    		$result['PublishId'] = $data->getData('PublishId');
    	}
    
    	return $result;
    }
    
    public function getImageCollectiveTitleById($pid){
        $sql = 'select Title from ImageCollective where Id=%s';
        $data = $this->query($sql,array($pid));
        $result = null;
        while($data->fetch_assoc()){
            $result = $data->getData('Title');
        }
        return $result;
    }
    
    public function getFeatureTitleById($fid){
        $sql = 'select t2.FeatureName,t1.Url from FeaturePublish t1 inner join FeatureDefine t2 on t1.FeatureDefineId=t2.Id where t1.Id=%s';
        $data = $this->query($sql,array($fid));
        $result = array();
        while($data->fetch_assoc()){
            $result['FeatureName'] = $data->getData('FeatureName');
            $result['Url'] = $data->getData('Url');
        }
    
        return $result;
    }
    
    public function getVideoTitleById($vid){
        $sql = 'select Title from Video where Id=%s';
        $data = $this->query($sql,array($vid));
        $result = null;
        while($data->fetch_assoc()){
            $result = $data->getData('Title');
        }
    
        return $result;
    }
    
    public function getTryProductBuId($id){
        $sql = 'select Title from TryProduct where Id=%s';
        $data = $this->query($sql,array($id));
        $result = null;
        while($data->fetch_assoc()){
            $result = $data->getData('Title');
        }
        
        return $result;
    }
    
    public function getProductNameById($id){
        $sql = 'select Name from Goods where Id=%s';
        $data = $this->query($sql,array($id));
        $result = null;
        while($data->fetch_assoc()){
            $result = $data->getData('Name');
        }
        
        return $result;
    }
}